<template>
    <div class="homeworkDetail clearfix">
        <h4 v-if="!zhuguan">{{ homework.homeworkName }}</h4>
        <input v-if="zhuguan" type="text" v-model="homework.homeworkName" @click="editName($event)" @keyup="this.edit = true">

        <p v-if="!zhuguan" v-html="(homework.homeworkDetail).split('\n').join('<br/>')"></p>

        <textarea cols="38" rows="10" v-if="zhuguan" v-model="homework.homeworkDetail" @keyup="this.edit = true"></textarea>

        <a v-if="zhuguan" class="upload">附件上传
            <input type="file" id="homeworkFile" @change="uploadOpen = true">
        </a>

        <a :href="homework.file" class="submit" style="margin-right:1em;">附件下载</a>

        <a v-if="edit" class="submit" @click="editWork()" style="margin-right:1em;">保存</a>

        <a v-if="uploadOpen" class="submit" @click="uploadFile()" style="margin-right:1em;">上传</a>
    </div>

</template>

<style lang="sass">
    .homeworkDetail{
        a{
            float: right;
        }
        input, textarea{
            border:none;
            width:100%;
        }
        input{
            color: #39f;
            font-size: 18px;
            margin-bottom: 20px;
        }
        textarea{
            height:200px;
        }
    }
</style>

<script>
    export default{
        props:['homework', 'zhuguan','homeworks'],
        data(){
            return {
                uploadOpen: false,
                edit:false,
                editHomework:{
                    oldName:'',
                    homeworkName:'',
                    homeworkDetail: '',
                    file:'',
                }
            }
        },

        methods:{
            editWork(){
                this.editHomework.homeworkName = this.homework.homeworkName.trim();
                this.editHomework.homeworkDetail = this.homework.homeworkDetail.trim();
                this.editHomework.file = this.homework.file;

                if (this.editHomework.homeworkName && this.editHomework.homeworkDetail) {
                    this.$http.post('/admin/work/editWork', this.editHomework).then(function (d) {
                        if (d.data == 'success') {
                            alert('修改成功');
                            this.edit = false;
                        }else if (d.data == 'Empty') {
                            alert('作业名或详情为空')
                        } else if (d.data == 'SameName') {
                            alert('新作业名与数据库中已有作业名重名')
                        }

                    }, function (err) {
                        alert('修改失败 err.status: ',err.status)
                    });
                }else {
                    alert('作业名或详情为空')
                }

            },
            editName($event){
               this.editHomework.oldName = $event.target.value;
            },
            uploadFile(){
                var file = document.getElementById('homeworkFile').files[0];
                if (file == undefined) {
                    alert('文件不能为空！');
                }else {
                    var homeworkName = this.homework.homeworkName;
                    var formData = new FormData();

                    formData.append('homeworkFile', file);
                    formData.append('homeworkName', homeworkName);

                    this.$http.post('/admin/work/homeworkFile', formData).then(function (res) {
                        alert(res.data);
                    }, function (err) {
                        alert('err', err)
                    });
                }
            },
        }
    }
</script>
